package com.xbe.control;

import com.xbe.common.core.domain.AjaxResult;
import com.xbe.equipment.domain.Equipment;
import com.xbe.equipment.service.IEquipmentService;
import com.xbe.netty.NettyServerHandler;
import com.xbe.order.domain.Order;
import com.xbe.order.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: lzt
 * @Desctription: 设备控制
 * @Date: Created in 2020-08-11 10:50
 * @Version: 1.0
 */
@RestController
@RequestMapping("/control")
public class EquipmentControl
{
    private static final Logger log = LoggerFactory.getLogger(EquipmentControl.class);

    @Autowired
    private IEquipmentService equipmentService;

    @Autowired
    private IOrderService orderService;

    //对时
    @GetMapping("/checkTime/{sim}")
    public AjaxResult checkTime(@PathVariable("sim") String sim)
    {
        Map map = NettyServerHandler.checkTime(sim);
        boolean flag = (boolean) map.get("flag");
        insertOrder(sim, map);

        if (flag)
        {
            return AjaxResult.success("发送成功");
        }
        return AjaxResult.error("发送失败,设备不在线");

    }

    private void insertOrder(String sim, Map map)
    {
        String orderName = (String) map.get("orderName");
        String state = (String) map.get("state");
        Order order = new Order();
        order.setSim(sim);
        order.setOrdername(orderName);
        order.setState(state);
        orderService.insertOrder(order);
    }

    //关闭模式
    @GetMapping("/work/close/{sim}")
    public AjaxResult send_workPattern_close(@PathVariable("sim") String sim)
    {
        //保存数据
        Equipment equipment = new Equipment();
        equipment.setSim(sim);
        equipment.setWorkPattern("0");
        equipmentService.updateEquipmentBySim(equipment);
        //索要数据,发现没有所需要的电压等数据的时候，那么就给设备发送数据

        int b = sendEmpty(sim);
        if (b == 1)
        {
            return AjaxResult.success("发送成功");

        }
        if (b == 2)
        {
            return AjaxResult.error("发送失败,设备不在线");
        }


        Map map = NettyServerHandler.send_workPattern_close(sim);
        boolean flag = (boolean) map.get("flag");
        insertOrder(sim, map);
        if (flag)
        {
            return AjaxResult.success("发送成功");
        }


        return AjaxResult.error("发送失败,设备不在线");

    }

    public int sendEmpty(String sim)
    {
        Equipment equipment1 = equipmentService.selectEquipmentBySim(sim);
        if (equipment1.getVoltage() == null || equipment1.getVisibility() == null || "".equals(equipment1.getVoltage()) || "".equals(equipment1.getVisibility()))
        {
            boolean b = NettyServerHandler.sendEmpty(sim);
            if (b)
            {
                //发送成功
                return 1;
            }
            //发送成功
            return 2;
        }
        //不需要发送命令，不需要索要
        return 0;
    }


    //自动模式
    @GetMapping("/work/auto/{sim}")
    public AjaxResult workClose(@PathVariable("sim") String sim)
    {
        //保存数据
        Equipment equipment = new Equipment();
        equipment.setSim(sim);
        equipment.setWorkPattern("2");
        equipmentService.updateEquipmentBySim(equipment);
        int b = sendEmpty(sim);
        if (b == 1)
        {
            return AjaxResult.success("发送成功");

        }
        if (b == 2)
        {
            return AjaxResult.error("发送失败,设备不在线");
        }
        Map map = NettyServerHandler.send_workPattern_auto(sim);
        boolean flag = (boolean) map.get("flag");
        insertOrder(sim, map);
        if (flag)
        {
            return AjaxResult.success("发送成功");
        }
        return AjaxResult.error("发送失败,设备不在线");

    }

    //手动模式--道路强化模式
    @PostMapping("/work/manual/road")
    public AjaxResult workClose(String sim, String brightness, String timePattern,
                                String startTime, String endTime)
    {
        //保存数据
        Equipment equipment = new Equipment();
        equipment.setSim(sim);
        equipment.setWorkPattern("1");
        equipment.setWorkSubPattern("0");
        equipment.setBrightness(Integer.valueOf(brightness));
        equipment.setTimePattern(timePattern);
        equipment.setStartTime(startTime);
        equipment.setEndTime(endTime);

        equipmentService.updateEquipmentBySim(equipment);
        int b = sendEmpty(sim);
        if (b == 1)
        {
            return AjaxResult.success("发送成功");

        }
        if (b == 2)
        {
            return AjaxResult.error("发送失败,设备不在线");
        }
        Map map = NettyServerHandler.send_workPattern_manual_road(sim, brightness, timePattern,
                                                                  startTime, endTime);
        boolean flag = (boolean) map.get("flag");
        insertOrdersAndtimePattern(sim, map, timePattern);

        if (flag)
        {
            return AjaxResult.success("发送成功");
        }
        return AjaxResult.error("发送失败,设备不在线");

    }

    private void insertOrdersAndtimePattern(String sim, Map map, String timePattern)
    {
        String orderName;
        if (timePattern.equals("0"))
        {
            orderName = (String) map.get("orderName") + "全部无效";
        }
        else if (timePattern.equals("1"))
        {
            orderName = (String) map.get("orderName") + "光敏使能";
        }
        else
        {
            orderName = (String) map.get("orderName") + "星历使能";
        }


        String state = (String) map.get("state");
        Order order = new Order();
        order.setSim(sim);
        order.setOrdername(orderName);
        order.setState(state);
        orderService.insertOrder(order);

    }

    //手动模式--主动诱导模式
    @PostMapping("/work/manual/induce")
    public AjaxResult workClose(String sim, String brightness, String frequency,
                                String timePattern, String startTime, String endTime)
    {
        //保存数据
        Equipment equipment = new Equipment();
        equipment.setSim(sim);
        equipment.setWorkPattern("1");
        equipment.setWorkSubPattern("1");
        equipment.setBrightness(Integer.valueOf(brightness));
        equipment.setFrequency(frequency);
        equipment.setTimePattern(timePattern);
        equipment.setStartTime(startTime);
        equipment.setEndTime(endTime);

        equipmentService.updateEquipmentBySim(equipment);
        int b = sendEmpty(sim);
        if (b == 1)
        {
            return AjaxResult.success("发送成功");

        }
        if (b == 2)
        {
            return AjaxResult.error("发送失败,设备不在线");
        }
        Map map = NettyServerHandler.send_workPattern_manual_induce(sim, brightness, frequency,
                                                                    timePattern, startTime,
                                                                    endTime);
        boolean flag = (boolean) map.get("flag");
        insertOrdersAndtimePattern(sim, map, timePattern);
        if (flag)
        {
            return AjaxResult.success("发送成功");
        }
        return AjaxResult.error("发送失败,设备不在线");

    }

    //手动模式--防止追尾模式
    @PostMapping("/work/manual/rearEnd")
    public AjaxResult workClose(String sim, String brightness, String frequency, String wake,
                                String timePattern, String startTime, String endTime)
    {
        //保存数据
        Equipment equipment = new Equipment();
        equipment.setSim(sim);
        equipment.setWorkPattern("1");
        equipment.setWorkSubPattern("2");
        equipment.setBrightness(Integer.valueOf(brightness));
        equipment.setFrequency(frequency);
        equipment.setWake(Integer.valueOf(wake));
        equipment.setTimePattern(timePattern);
        equipment.setStartTime(startTime);
        equipment.setEndTime(endTime);

        equipmentService.updateEquipmentBySim(equipment);
        int b = sendEmpty(sim);
        if (b == 1)
        {
            return AjaxResult.success("发送成功");

        }
        if (b == 2)
        {
            return AjaxResult.error("发送失败,设备不在线");
        }
        Map map = NettyServerHandler.send_workPattern_manual_rearEnd(sim, brightness, frequency,
                                                                     wake, timePattern, startTime
                , endTime);
        boolean flag = (boolean) map.get("flag");
        insertOrdersAndtimePattern(sim, map, timePattern);

        if (flag)
        {
            return AjaxResult.success("发送成功");
        }
        return AjaxResult.error("发送失败,设备不在线");

    }


}
